/*
 * OREKIT-X
 * Copyright 2002-2008 CS Communication & Systemes
 * 
 * Parts of this software package have been licensed to CS
 * Communication & Systemes (CS) under one or more contributor license
 * agreements.  See the NOTICE file distributed with this work for
 * additional information.
 *  
 * This is an experimental copy of OREKIT from www.orekit.org.
 * Please use the original OREKIT from orekit.org for normal work
 * unrelated to this research project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You
 * may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package ore.io;

import ore.bodies.JPLConstants;
import ore.io.DataLoader;
import ore.errors.OrekitException;
import ore.time.AbsoluteDate;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;

/** 
 * Load constants from a JPL ephemerides binary file (DE 405, DE 406).
 * 
 * @author Luc Maisonobe
 * @author John Pritchard
 * @version $Revision$ $Date$
 */
public class JPLConstantsLoader
    extends ore.bodies.JPLConstants
    implements DataLoader 
{

    private volatile boolean loading = true;


    public JPLConstantsLoader(){
        super();
    }


    /** {@inheritDoc} */
    public boolean stillAcceptsData() {

        return this.loading;
    }

    /** {@inheritDoc} */
    public void loadData(final InputStream input, final String name)
        throws IOException, ParseException, OrekitException
    {
        final byte[] first  = JPLEphemeridesLoader.ReadFirstRecord(input, name);
        final byte[] second = new byte[first.length];
        if (!JPLEphemeridesLoader.ReadInRecord(input, second, 0)) {
            throw new OrekitException(JPLEphemeridesLoader.HEADER_READ_ERROR, name);
        }
        else {
            if (JPLConstantsLoader.ParseConstants(this,first,second))
                JPLConstants.Instance = this;
            else
                throw new OrekitException(JPLEphemeridesLoader.HEADER_READ_ERROR, name);
        }
    }

    protected final static boolean ParseConstants(JPLConstants that, byte[] first, byte[] second){
        int changed = that.size();
        String name;
        double value;
        for (int i = 0; i < 400; ++i) {
            name = JPLEphemeridesLoader.ExtractString(first, 252 + i * 6, 6);
            if (0 == name.length())
                return (0 < i);
            else {
                value = JPLEphemeridesLoader.ExtractDouble(second, 8 * i);
                that.set(name, value);
            }
        }
        return (changed < that.size());
    }
}
